package com.powernode.controller;

import com.powernode.common.Constants;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.awt.*;
import java.io.File;
import java.io.FileInputStream;

@RestController
@RequestMapping("download")
public class downloadController {

    @RequestMapping("loadImag.action")
    public  void download(String path, HttpServletResponse response) throws Exception {
        File file = new File(Constants.ROOTPATH, path);
        FileInputStream fis = new FileInputStream(file);
        int len =-1;
        byte[] bytes = new byte[1024];
        ServletOutputStream ops = response.getOutputStream();
        response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
        while ((len=fis.read(bytes))!=-1){
            ops.write(bytes,0,len);
            ops.flush();
        }
        ops.close();
        fis.close();
    }
}

